<template>
  <div class="residentBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <h3>管理员列表</h3>
      </div>
      <div class="text item">
        <el-input placeholder="请输入关键词" v-model="key" clearable></el-input>
      </div>
      <el-button type="primary" @click="Administrator">查询</el-button>
      <el-button type="success" @click="Addadministrator"
        >添加</el-button
      >
    </el-card>
    <administratorDialog
      :isEdit="isEdit"
      :item="item"
      :centerDialogVisible="centerDialogVisible"
      @close="close('centerDialogVisible')"
    ></administratorDialog>
    <administratorTable
    :list="list"
    :tableList='tableList'
    @DelAdministrator="DelAdministrator"
    @Updata="Updata"
    ></administratorTable>
    <page
      :total="total"
      :pageSize="pageSize"
      @changePsize="handleSizeChange"
      @changePage="handleCurrentChange"
    ></page>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import {
  administrator,
  Addadministrator,
  Deladministrator,
} from "../api/administrator";
import administratorDialog from "../components/admininstrator/Dialog.vue";
import Page from "../components/common/page2.vue";
import administratorTable from '../components/admininstrator/table.vue'
export default { 
  //import引入的组件需要注入到对象中才能使用
  components: {
    administratorDialog,
    Page,
    administratorTable
  },
  data() {
    //这里存放数据
    return {
      list: [],
      key: "",
      page: 1,
      pageSize:5,
      total: 0,
      centerDialogVisible: false,
      isEdit: false,
      item: {},
      tableList:[
        {
          label:"ID",
          prop:"id"
        },
        {
          label:"登录账号",
          prop:"username"
        },
        {
          label:"姓名",
          prop:"name"
        },
        {
          label:"手机号",
          prop:"tel"
        },
        {
          label:"类型",
          prop:"typeName"
        },
        {
          label:"操作",
          type:"button",
          button:{
            updata:true,
            delete:true
          }
        }
      ]
    };
  },
  //方法集合
  methods: {
    //列表
    async Administrator() {
      const { data } = await administrator({
        key: this.key,
        page: this.page,
        psize: this.pageSize,
      });
      this.list = data.list;
      this.total = data.counts;
    },
    //分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.Administrator();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.Administrator();
    },
    // 删除
    DelAdministrator(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(async () => {
          const { data } = await Deladministrator({ id: id });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.Administrator();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    Addadministrator(){
      this.centerDialogVisible = true
      this.isEdit = false
    },
    Updata(val) {
      this.item = val;
      this.centerDialogVisible = true;
      this.isEdit = true;
    },
    close(show) {
      this[show] = false;
      this.Administrator();
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.Administrator();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
};
</script>
<style lang="less">
.residentBox {
  .box-card {
    padding-bottom: 20px;
    .item {
      float: left;
      :nth-child(2) {
        width: 180px;
        margin-left: 10px;
      }
      :nth-child(3) {
        margin-left: 30px;
        margin-right: 10px;
      }
    }
    .el-select {
      float: left;
    }
    .el-button {
      margin-left: 30px;
      width: 100px;
    }
  }
  .btn {
    margin-top: 14px;
    .el-button {
      width: 100px;
    }
  }
  .el-table {
    margin-top: 20px;
  }
  .el-pagination {
    margin-top: 10px;
    margin-left: 28%;
  }
}
</style>